class ItemGroup extends eui.Component implements eui.UIComponent {
	public constructor(glv: GameLevel) {
		super();
		this._gameLevel = glv;
		ItemGroup._ins = this;
	}	
	public ItemGroup: eui.Group;

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	protected childrenCreated(): void {
		super.childrenCreated();
		this.init();
	}	

	public get canTouch(){ return this._canTouch; }
	public set canTouch(touch:boolean){ this._canTouch = touch; }
	public static get ins() : ItemGroup { return ItemGroup._ins; }

	// 开始按下
	private onTouchBegin(e: egret.TouchEvent) {
		let self = this;

		if(!self._canTouch) { console.log("还在做动画！"); return;}

		self._canTouch = false;
		self._old_X = e.stageX;
		self._old_Y = e.stageY;
		self._oldNode = self.getNodeByTouchPos(self._old_X, self._old_Y);

		self._isTouchBegin = true;
		console.log("按下按钮！");
	}

	// 抬起
	private onTouchEnd(e: egret.TouchEvent) {
		let self = this;
		if(!self._isTouchBegin || self._canTouch) return;

		self._canTouch = true;
		self._cur_X = e.stageX;
		self._cur_Y = e.stageY;

		let dx = self._cur_X - self._old_X;
		let dy = self._cur_Y - self._old_Y;

		let oldNode = self._oldNode;

		let oldX = self._oldNode.x_pos;
		let oldY = self._oldNode.y_pos;

		self._isTouchBegin = false;
		console.log("抬起按钮！");
		if (Math.abs(dx) > Math.abs(dy)) {
			// 左右移动
			if (dx > 40) {
				console.log("往右边移动！");
				oldX += 1;
			}
			else if(dx < -40) {
				console.log("往左边移动！");
				oldX -= 1;
			}else{ return; }
		}
		else if (Math.abs(dx) < Math.abs(dy)) {
			// 上下移动
			if (dy > 40) {
				console.log("往下边移动！");
				oldY += 1;
			}
			else if(dy < -40) {
				console.log("往上边移动！");
				oldY -= 1;
			}else{ return; }
		}
		else { console.log("不操作！"); return; } // 不做操作

		self._curNode = self.getNodeByPos(oldX, oldY);
		JcEvent.ins.NotifyEvent(XXLEventName.swapeNode, self._oldNode, self._curNode);

		self._oldNode = null;
		self._curNode = null;
	}

	// 通过x y 索引获取节点数据
	private getNodeByPos(xp : number, yp : number) : XXLNode{
		var node = this._gameLevel.getNodeByPos(xp, yp);

		return node;
	}

	// 通过手指触碰坐标获取节点
	private getNodeByTouchPos(x: number, y: number): XXLNode {
		var dx = x / 80;
		var dy = y / 80;

		var xCnt: number = 0;
		var yCnt: number = 0;

		xCnt = Math.floor(dx);
		yCnt = Math.floor(dy);

		var node = this._gameLevel.getNodeByPos(xCnt, yCnt);

		return node;
	}

	private onXCItem(en : string){
		// 开始消除
		this._canTouch = false;
    }
    private onXCItemEnd(en : string){
		// 消除完成
		// 开始下落
		console.log("开始下落节点！");
		JcEvent.ins.NotifyEvent(XXLEventName.dropNode);
		this._canTouch = true;
    }

    private onSwapeItem(en : string){
		// 开始交换的动画
		this._canTouch = false;
    }
    private onSwapeItemEnd(en : string){
		// 交换动画完成
		// 开始检测消除
		console.log("开始检测所有节点！");
		JcEvent.ins.NotifyEvent(XXLEventName.checkAll);
		this._canTouch = true;
	}


    private onDoDropItem(en : string){
		// 开始下落的动画
		this._canTouch = false;
    }
    private onDropItemEnd(en : string){
		// 下落动画完成,下落完成后自动检测
		this._canTouch = true;
		egret.setTimeout(this.testRand, this, 3000);
	}

	private testRand(){
		JcEvent.ins.NotifyEvent(XXLEventName.dropNodeEnd);
	}

	private initEvent(){
		let self = this;
		self.ItemGroup.addEventListener(egret.TouchEvent.TOUCH_BEGIN, self.onTouchBegin, self);
		self.ItemGroup.addEventListener(egret.TouchEvent.TOUCH_END, self.onTouchEnd, self);

		// JcEvent.ins.RegistEvent(XXLEventName.dropNodeEnd, self.onDropNodeEnd, self);
		// JcEvent.ins.RegistEvent(XXLEventName.dropItem, self.onDropItem, self);

		// 消除
		JcEvent.ins.RegistEvent(XXLEventName.onXCItem, self.onXCItem, self);
		JcEvent.ins.RegistEvent(XXLEventName.xcItemEnd, self.onXCItemEnd, self);
		
		// 交换
		JcEvent.ins.RegistEvent(XXLEventName.onSwapeItem, self.onSwapeItem, self);
		JcEvent.ins.RegistEvent(XXLEventName.swapeItemEnd, self.onSwapeItemEnd, self);

		// 下落
		JcEvent.ins.RegistEvent(XXLEventName.onDropItem, self.onDoDropItem, self);
		JcEvent.ins.RegistEvent(XXLEventName.dropItemEnd, self.onDropItemEnd, self);
	}

	// 初始化
	private init() {
		let self = this;
		
		self._canTouch = true;
		self._isTouchBegin = false;

		self.initEvent();		
	}

	private _canTouch: boolean;
	private _isTouchBegin : boolean;

	private _old_X: number;
	private _old_Y: number;

	private _cur_X: number;
	private _cur_Y: number;

	private _oldNode: XXLNode;
	private _curNode: XXLNode;

	private _gameLevel: GameLevel;
	private static _ins : ItemGroup;
}

class TestState{


	private testState(){
		
	}
}


enum NodeStatus{
	SwapeNode,
	SwapeItemActor,
	SwapeItemEnd,
	XCNode,	
	XCItemActor,
	XCItemEnd,
	
}